package com.smarthome.web.property.controller.deviceManager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smarthome.core.deviceManager.EquipmentProducerService;
import com.smarthome.domain.bean.ShEquipmentProducer;
import com.smarthome.domain.common.Result;
import com.smarthome.domain.vo.boss.Combobox;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;

/**
 * 设备供应商管理
 * @author szc
 *
 */
@Controller
@RequestMapping("/equipmentProducerController")
public class EquipmentProducerController {

    @Resource
    private EquipmentProducerService equipmentProducerService;

    /**
     * 获取设备供应商信息列表
     * @param accountLog
     * @param page
     * @return
     */
    @RequestMapping("/dataGrid")
    @ResponseBody
    public DataGrid dataGrid(ShEquipmentProducer equipmentProducer, PageHelper page) {
        return equipmentProducerService.dataGrid(equipmentProducer, page);
    }

    /**
     * 跳转到添加页面
     * 
     * @return
     */
    @RequestMapping("/addPage")
    public String addPage(HttpServletRequest request) {
        return "/deviceManager/deviceSupplierAdd";
    }

    /**
     * 新增设备供应商
     * @param deviceSupplier
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public Result add(ShEquipmentProducer equipmentProducer) {
        try {
            // 判断是否已经存在
            if (equipmentProducer.getProBzCode() != null && StringUtils.isNotBlank(equipmentProducer.getProBzCode())) {
                ShEquipmentProducer equipmentProducerOld = new ShEquipmentProducer();
                equipmentProducerOld.setProBzCode(equipmentProducer.getProBzCode());
                List<ShEquipmentProducer> list = equipmentProducerService.queryShEquipmentProducerList(equipmentProducerOld);
                if (list != null && list.size() > 0) {
                    return Result.failure("该供应商已存在！");
                }
            } else {
                return Result.failure("请选择【唯一标识】值！");
            }
            equipmentProducerService.add(equipmentProducer);
            return Result.success("添加成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure("添加供应商信息失败！");
        }
    }

    /**
     * 跳转到编辑页面
     * @param req
     * @param id
     * @return
     */
    @RequestMapping("/editPage")
    public String editPage(HttpServletRequest req, Long id) {
        ShEquipmentProducer deviceSupplier = equipmentProducerService.queryShDeviceSupplierById(id);
        req.setAttribute("deviceSupplier", deviceSupplier);
        return "/deviceManager/deviceSupplierEdit";
    }

    /***
     * 修改
     * @param deviceSupplier
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Result edit(ShEquipmentProducer equipmentProducer) {
        try {
            // 判断是否已经存在
            if (equipmentProducer.getProBzCode() != null && StringUtils.isNotBlank(equipmentProducer.getProBzCode())) {
                ShEquipmentProducer equipmentProducerOld = new ShEquipmentProducer();
                equipmentProducerOld.setProBzCode(equipmentProducer.getProBzCode());
                List<ShEquipmentProducer> list = equipmentProducerService.queryShEquipmentProducerList(equipmentProducerOld);
                if (list != null && list.size() > 0) {
                    for (ShEquipmentProducer obj : list) {
                        // 如果主键不同，说明不是当前
                        if (!obj.getId().toString().equals(equipmentProducer.getId().toString())) {
                            return Result.failure("该供应商【唯一标识】已存在！");
                        }
                    }
                }
            }
            equipmentProducerService.edit(equipmentProducer);
            return Result.success("更改成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure("更改出现异常！");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Result delete(Long id) {
        ShEquipmentProducer equipmentProducer = equipmentProducerService.queryShDeviceSupplierById(id);
        equipmentProducer.setProState("3");
        equipmentProducerService.saveOrUpdate(equipmentProducer);
        return Result.success("删除成功！");
    }

    /**
     * 得到设备供应商下拉选项
     * @return
     */
    @RequestMapping("/getComboData")
    @ResponseBody
    public Map<String, Object> getComboData() {
        Map<String, Object> result = new HashMap<>();
        List<Combobox> deviceSupplier = equipmentProducerService.getShEquipmentProducerCombobox(false);
        // List<Combobox> results = equipmentCommLogService.results(false);
        result.put("deviceSupplier", deviceSupplier);
        // result.put("results", results);
        return result;
    }

}
